/******************************************************************
 * Created by: DO HOANG HAI                                       
 * Date of Creation:                                               
 * Date of last modification: 1st/Sep/2007
 *                                                                
 * Description:                                 
 * ...
 ******************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Pbdms.Forms.Views
{
    public partial class FrmBuddyList : Form
    {
        ArrayList arlBuddies;
        bool isCellValueChanged = false;
        bool enableGrdvModification = true;
        public FrmBuddyList()
        {
            InitializeComponent();
            Pbdms.Common.Global.paintBackGround(this);
            arlBuddies = BusinessLogic.DataController.BuddyController.create().getListOfBuddies();

            showListOfBuddies();           
            
            grdvBuddies.CellValueChanged += new DataGridViewCellEventHandler(grdvBuddies_CellValueChanged);
            lblAddReport.Text = "";
        }
        void grdvBuddies_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (enableGrdvModification == true)
            {
                if (e.ColumnIndex == 2)
                {
                    if (!Common.Global.isAnIPAddress(grdvBuddies.CurrentCell.Value.ToString()))
                    {
                        Common.Global.showMsgOneButton("Invalid value of IpAddress (example valid IP Address: 172.20.25.58)", 'C');
                        grdvBuddies.CurrentCell.Value = ((Common.DataObject.Buddy)arlBuddies[grdvBuddies.CurrentRow.Index]).IpAdd.ToString();
                        //the cell's value won't be changed.
                    }
                    else
                    {
                        isCellValueChanged = true;
                    }
                }
                else
                {
                    isCellValueChanged = true;
                }                
            }
        }        

        private void showListOfBuddies()
        {
            //clear all data in the grid view            
            grdvBuddies.Columns.Clear();

            //build the data table for gridview
            DataTable dt = new DataTable();
            //add 10 columns
            for (int i = 0; i < 3; i++)
            {
                dt.Columns.Add();
            }

            int iCount = 1;
            if (arlBuddies  != null && arlBuddies.Count>0)
            {
                //for each item, add one row
                foreach (Common.DataObject.Buddy buddyObj in arlBuddies)
                {
                    //ArrayList arlOfBibTexese = itemObj.ItemObj.BibTexes;

                    string[] itemArray = new string[]{iCount.ToString(),buddyObj.Name,buddyObj.IpAdd};
                    dt.Rows.Add(itemArray);
                    //next row
                    iCount++;
                }
            }

            grdvBuddies.DataSource = dt;
            //setReadOnlyToNetworkItems();
            customizeGridView();
        }

        private void customizeGridView()
        {
            for (int i = 0; i < 3; i++)
            {
                switch (i)
                {
                    case 0: grdvBuddies.Columns[0].HeaderText = "Index"; grdvBuddies.Columns[0].ReadOnly = true; grdvBuddies.Columns[0].Width = 50; break;
                    case 1: grdvBuddies.Columns[1].HeaderText = "Name"; grdvBuddies.Columns[1].Width = 220; break;
                    case 2: grdvBuddies.Columns[2].HeaderText = "IP address"; grdvBuddies.Columns[2].Width = 120; break;                    
                }
            }            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            
        }

        private void saveChanges()
        {
            DataTable dt = (DataTable)grdvBuddies.DataSource;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int itemIndex = int.Parse(grdvBuddies.Rows[i].Cells[0].Value.ToString()) - 1;
                               
                Common.DataObject.Buddy bdd = (Common.DataObject.Buddy)arlBuddies[itemIndex];
                string name = grdvBuddies.Rows[i].Cells[1].Value.ToString();
                string ip = grdvBuddies.Rows[i].Cells[2].Value.ToString();
                if (!name.Equals(bdd.Name) || !ip.Equals(bdd.IpAdd))
                {
                    bdd.Name = name;
                    bdd.IpAdd = ip;
                    BusinessLogic.DataController.BuddyController.create().updateBuddyToDB(bdd);
                }                
            }

        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Common.DataObject.Buddy bdd = (Common.DataObject.Buddy)arlBuddies[grdvBuddies.CurrentRow.Index];
            BusinessLogic.DataController.BuddyController.create().deleteBuddyFromDB(bdd);
            arlBuddies.Remove(bdd);
            //disable gridview's cellValueChanged event handler
            enableGrdvModification = false;
            showListOfBuddies();
            //re-enable the event
            enableGrdvModification = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (Common.Global.isAnIPAddress(txtIP.Text))
            {             
                Common.DataObject.Buddy bdd = BusinessLogic.DataController.BuddyController.create().appendBuddyToDB(txtName.Text, txtIP.Text);
                this.arlBuddies.Add(bdd);
                
                lblAddReport.Text = "\"" + txtName.Text + "\" has been added";
                txtName.Text = "";
                txtIP.Text = "";
                enableGrdvModification = false;
                showListOfBuddies();
                enableGrdvModification = true;

            }
            else
            {
                Common.Global.showMsgOneButton("Invalid IP address!\r\nCorrect (e.g: 172.20.25.58) and try again", 'C');
            }
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            if (isCellValueChanged)
            {
                int answer= Common.Global.showMsgTwoButtons("Would you like to save changes before close?",Common.GUI.MsgBoxTwoButtons.YES_NO,'I');
                if (answer==1)
                    saveChanges();
                isCellValueChanged = false;
            }
            this.Close();
        }
        
    }
}